<?php

//   -------------------------------------------------------------------------------
//  |                  net2ftp: a web based FTP client                              |
//  |              Copyright (c) 2003-2006 by David Gartner                         |
//  |                                                                               |
//  | This program is free software; you can redistribute it and/or                 |
//  | modify it under the terms of the GNU General Public License                   |
//  | as published by the Free Software Foundation; either version 2                |
//  | of the License, or (at your option) any later version.                        |
//  |                                                                               |
//   -------------------------------------------------------------------------------





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function net2ftp_module_sendHttpHeaders() {

// --------------
// This function sends HTTP headers
// --------------

//	global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result;
	
} // end net2ftp_sendHttpHeaders

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function net2ftp_module_printJavascript() {

// --------------
// This function prints Javascript code and includes
// --------------

	global $net2ftp_settings, $net2ftp_globals;

	if (isset($_POST["textareaType"]) == true) { $textareaType = validateTextareaType($_POST["textareaType"]); }
	else                                       { $textareaType = ""; }

	if ($textareaType == "" || $textareaType == "plain") {
		echo "<script type=\"text/javascript\"><!--\n";	
		echo "function TabText() {\n";
		echo "	if (event != null) {\n";
		echo "		if (event.srcElement) {\n";
		echo "			if (event.srcElement.value) {\n";
		echo "				if (event.keyCode == 9) {\n";
		echo "					if (document.selection != null) {\n";
		echo "						document.selection.createRange().text = '\\t';\n";
		echo "						event.returnValue = false;\n";
		echo "					}\n";
		echo "					else {\n";
		echo "						event.srcElement.value += '\\t';\n";
		echo "						return false;\n";
		echo "					}\n";
		echo "				}\n";
		echo "			}\n";
		echo "		}\n";
		echo "	}\n";
		echo "}\n";
		echo "//--></script>\n";
	}

// Include
	echo "<script type=\"text/javascript\" src=\"". $net2ftp_globals["application_rootdir_url"] . "/modules/edit/edit.js\"></script>\n";

} // end net2ftp_printJavascript

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function net2ftp_module_printCss() {

// --------------
// This function prints CSS code and includes
// --------------

	global $net2ftp_settings, $net2ftp_globals;

// Include
	echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"". $net2ftp_globals["application_rootdir_url"] . "/skins/" . $net2ftp_globals["skin"] . "/css/main.css\" />\n";

} // end net2ftp_printCssInclude

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function net2ftp_module_printBodyOnload() {

// --------------
// This function prints the <body onload="" actions
// --------------

//	global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result;
//	echo "";

} // end net2ftp_printBodyOnload

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function net2ftp_module_printBody() {

// --------------
// This function prints the edit screen
// For screen == 1, the file is read from the FTP server
// For screen == 2, the textarea is changed, the file is not read from the FTP server but comes from the HTMl form
// For screen == 3, the file is saved to the FTP server
// --------------

// -------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------
	global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result;
	if (isset($_POST["textareaType"]) == true)  { $textareaType = validateTextareaType($_POST["textareaType"]); }
	else                                        { $textareaType = ""; }
	if (isset($_POST["text"]) == true)          { $text = $_POST["text"]; }
	else                                        { $text = ""; }
	if (isset($_POST["text_splitted"]) == true) { $text_splitted = $_POST["text_splitted"]; }
	else                                        { $text_splitted = ""; }

// -------------------------------------------------------------------------
// Variables for all screens
// -------------------------------------------------------------------------

// Form name, back and forward buttons
	$formname = "EditForm";
	$back_onclick = "document." . $formname . ".state.value='browse'; document." . $formname . ".state2.value='main';document." . $formname . ".submit();";

// Language 
	if     ($net2ftp_globals["language"] == "cs") { $fckeditor_language = "cs"; }
	elseif ($net2ftp_globals["language"] == "de") { $fckeditor_language = "de"; }
	elseif ($net2ftp_globals["language"] == "es") { $fckeditor_language = "es"; }
	elseif ($net2ftp_globals["language"] == "fr") { $fckeditor_language = "fr"; }
	elseif ($net2ftp_globals["language"] == "it") { $fckeditor_language = "it"; }
	elseif ($net2ftp_globals["language"] == "ja") { $fckeditor_language = "ja"; }
	elseif ($net2ftp_globals["language"] == "nl") { $fckeditor_language = "nl"; }
	elseif ($net2ftp_globals["language"] == "pl") { $fckeditor_language = "pl"; }
	elseif ($net2ftp_globals["language"] == "pt") { $fckeditor_language = "pt"; }
	elseif ($net2ftp_globals["language"] == "ru") { $fckeditor_language = "ru"; }
	elseif ($net2ftp_globals["language"] == "tc") { $fckeditor_language = "zh"; }
	elseif ($net2ftp_globals["language"] == "vi") { $fckeditor_language = "vi"; }
	elseif ($net2ftp_globals["language"] == "zh") { $fckeditor_language = "zh-cn"; }
	else                                          { $fckeditor_language = "en"; }

// URL
	$fckeditor_basepath = $net2ftp_globals["application_rootdir_url"] . "/plugins/fckeditor/";

// -------------------------------------------------------------------------
// Variables for screen 1
// Read the remote file (edit), or read the local template (new file)
// -------------------------------------------------------------------------
	if ($net2ftp_globals["screen"] == 1) {

// Template file
		$templatefile = $net2ftp_globals["application_rootdir"] . "/modules/edit/template.txt";

// Edit: read the file from the FTP server
		if ($net2ftp_globals["state2"] == "") {
			$text = ftp_readfile("", $net2ftp_globals["directory"], $net2ftp_globals["entry"]);
			if ($net2ftp_result["success"] == false) { return false; }
		}

// New file: read the template file
		elseif ($net2ftp_globals["state2"] == "newfile") {
			$handle = fopen($templatefile, "r"); // Open the local template file for reading only
			if ($handle == false) { 
				$errormessage = __("Unable to open the template file");
				setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
				return false;
			}

			clearstatcache(); // for filesize

			$text = fread($handle, filesize($templatefile));
			if ($text == false) { 
				$errormessage = __("Unable to read the template file");
				setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
				return false;
			}

			@fclose($handle);
		}

// Save status
		$savestatus = __("Status: This file has not yet been saved");

	}

// -------------------------------------------------------------------------
// Variables for screen 2
// Change the textarea without saving the changes to the FTP server
// -------------------------------------------------------------------------
	elseif ($net2ftp_globals["screen"] == 2) {

// For HTML WYSIWYG editors, split the HTML
		if (($textareaType == "tinymce" || $textareaType == "fckeditor") && $text_splitted == "") {
			$text_splitted = splitHtml($text);
		}
// For other textareas, join the HTML
		elseif (($textareaType == "plain" || $textareaType == "helene") && $text == "" && isset($text_splitted["top"]) == true) {
			$text  = $text_splitted["top"];
			$text .= $text_splitted["middle"];
			$text .= $text_splitted["bottom"];
		}

// Save status
		$savestatus = __("Status: This file has not yet been saved");
	
	}

// -------------------------------------------------------------------------
// Variables for screen 3
// Save the changes to the FTP server
// -------------------------------------------------------------------------
	elseif ($net2ftp_globals["screen"] == 3) {

// Check if a filename is specified
		if (strlen($net2ftp_globals["entry"])<1) { 
			$errormessage = __("Please specify a filename"); 
			setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
			return false;
		}

// For HTML WYSIWYG editors, join the HTML
		if ($textareaType == "tinymce" || $textareaType == "fckeditor") {
			$text  = $text_splitted["top"];
			$text .= $text_splitted["middle"];
			$text .= $text_splitted["bottom"];
		}

// Write the string to the FTP server
// Note: this function also replaces CarriageReturn+LineFeed by LineFeed
		ftp_writefile("", $net2ftp_globals["directory"], $net2ftp_globals["entry"], $text);
		if ($net2ftp_result["success"] == false) { 
			setErrorVars(true, "", "", "", ""); // Continue anyway and print warning message
			$savestatus = __("Status: <b>This file could not be saved</b>"); 
		}
		else { 
			$mytime = mytime();
			$ftpmode = ftpAsciiBinary($net2ftp_globals["entry"]);
			if ($ftpmode == FTP_ASCII)      { $printftpmode = "FTP_ASCII"; }
			elseif ($ftpmode == FTP_BINARY) { $printftpmode = "FTP_BINARY"; }
			$savestatus = __("Status: Saved on <b>%1\$s</b> using mode %2\$s", $mytime, $printftpmode); 
		}

	}


// -------------------------------------------------------------------------
// Convert special characters to HTML entities
// -------------------------------------------------------------------------

// Plain textarea
	if ($textareaType == "" || $textareaType == "plain") {
		$text = htmlspecialchars($text, ENT_QUOTES);
	}

// FCKEditor
	elseif ($textareaType == "fckeditor") {
		$text_splitted["top"] = htmlspecialchars($text_splitted["top"], ENT_QUOTES);
		$text_splitted["bottom"] = htmlspecialchars($text_splitted["bottom"], ENT_QUOTES);
// Do not encode the middle part, this is done by FCKEditor itself
//		$text_splitted["middle"] = htmlspecialchars($text_splitted["middle"], ENT_QUOTES);
	}

// TinyMCE
	elseif ($textareaType == "tinymce") {
		$text_splitted["top"] = htmlspecialchars($text_splitted["top"], ENT_QUOTES);
		$text_splitted["middle"] = htmlspecialchars($text_splitted["middle"], ENT_QUOTES);
		$text_splitted["bottom"] = htmlspecialchars($text_splitted["bottom"], ENT_QUOTES);
	}

// Helene
	elseif ($textareaType == "helene") {
		$text_syntax = syntaxTextareaEncode($text);
		$text        = htmlspecialchars($text, ENT_QUOTES);
	}


// -------------------------------------------------------------------------
// Print the output
// -------------------------------------------------------------------------
	require_once($net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/edit.template.php");


} // End net2ftp_printBody

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function printTextareaSelect() {

// --------------
// This function prints a select with the available textareas
// --------------

// -------------------------------------------------------------------------
// Global variables
// -------------------------------------------------------------------------
	global $net2ftp_globals;
	$pluginProperties = getPluginProperties();
	if (isset($_POST["textareaType"]) == true) { $textareaType = validateTextareaType($_POST["textareaType"]); }
	else                                       { $textareaType = ""; }

	$filename_extension = get_filename_extension($net2ftp_globals["entry"]);

// Convert *htm* to html
	if (strpos($filename_extension, "htm") !== false) { $filename_extension = "html"; }

	$onchange = "document.EditForm.screen.value=2;document.EditForm.textareaType.value=document.EditForm.textareaSelect.options[document.EditForm.textareaSelect.selectedIndex].value; document.EditForm.submit();";
	echo "<select name=\"textareaSelect\" id=\"textareaSelect\" onChange=\"$onchange\">\n";

	if ($textareaType == "" || $textareaType == "plain") { $plainselected = "selected"; }
	echo "<option value=\"plain\" $plainselected>Normal textarea</option>\n";

	while(list($pluginName, $value) = each($pluginProperties)) {
// Print only the plugins which have 'use' set to yes
//                        which are textareas
//                        which are suitable for this browser
//                        which are suitable for this type of file
		if ($pluginProperties[$pluginName]["use"] == "yes" && $pluginProperties[$pluginName]["type"] == "textarea" && in_array($net2ftp_globals["browser_agent"], $pluginProperties[$pluginName]["browsers"]) == true && in_array($filename_extension, $pluginProperties[$pluginName]["filename_extensions"]) == true) {
			if ($pluginName == $textareaType) { $selected = "selected"; }
			else                              { $selected = ""; }
			echo "<option value=\"$pluginName\" $selected>" . $pluginProperties[$pluginName]["label"] . "</option>\n";
		} // end if
	} // end while

	echo "</select>\n";

} // End function printTextareaSelect

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function syntaxTextareaEncode($string) {

// --------------
// Replace tabs, line-feeds and carriage-returns by \t and \n respectively.
// --------------

	$tab = chr(9);
	$lf = chr(10);
	$cr = chr(13);

	$newstring = $string;

// Replace \' and '
	$newstring = str_replace("\'", "\\\'", $newstring);
	$newstring = str_replace("'", "\'", $newstring);

// Replace $cr$lf by $lf
	$newstring = str_replace("$cr$lf", "$lf", $newstring);

// Replace $lf and $tab
	$newstring = str_replace($lf, "\\n", $newstring);
	$newstring = str_replace($tab, "\\t", $newstring);

	return $newstring;

} // end syntaxTextareaEncode

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************





// **************************************************************************************
// **************************************************************************************
// **                                                                                  **
// **                                                                                  **

function splitHtml($text) {

// --------------
// This function reads an HTML file, and returns the head and the body parts.
// --------------

	$pattern = "/(.*?)<body([^\\>])*\\>(.*)\\<\\/body\\>(.*)/is";
//                 Everything before the <body tag ==> {1}
//                      Start of the body tag <body
//                           Body tag contents ==> {2}
//                                       Body contents ==> {3}
//                                             </body> tag
//                                                        Stuff after the </body> tag ==> {4}

	$preg_match_result = preg_match($pattern, $text, $matches);
	if ($preg_match_result != 0) {
		$text_splitted["top"]    = $matches[1];
		$text_splitted["top"]   .= "<body" . $matches[2] . ">";
		$text_splitted["middle"] = $matches[3];
		$text_splitted["bottom"] = "</body>" . $matches[4];
	}
	else {
		$text_splitted["top"]    = "";
		$text_splitted["middle"] = $text;
		$text_splitted["bottom"] = "";
	}

	return $text_splitted;

} // end splitHtml

// **                                                                                  **
// **                                                                                  **
// **************************************************************************************
// **************************************************************************************

?>